package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.EduCourseDescription;
import com.atguigu.eduservice.entity.frontVo.CourseFrontVo;
import com.atguigu.eduservice.entity.vo.CourseInfoVo;
import com.atguigu.eduservice.entity.vo.CoursePublishVo;
import com.atguigu.eduservice.entity.vo.CourseWebVo;
import com.atguigu.eduservice.mapper.EduCourseMapper;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduCourseDescriptionService;
import com.atguigu.eduservice.service.EduCourseService;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-06-22
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Autowired
    EduCourseDescriptionService eduCourseDescriptionService;

    @Autowired
    EduChapterService eduChapterService;

    @Autowired
    EduVideoService eduVideoService;
    @Override
    public String addCourseInfo(CourseInfoVo courseInfoVo) {
        EduCourse eduCourse=new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        //保存课程信息
        int insert = this.baseMapper.insert(eduCourse);
        if(insert <= 0){
            throw new GuliException(20001,"课程信息存储失败");
        }
        //获取保存后的课程id
        String courseId = eduCourse.getId();
        EduCourseDescription courseDescription=new EduCourseDescription();
        courseDescription.setDescription(courseInfoVo.getDescription());
        //由于课程与课程藐视是一对一关系，课程id跟描述id保持一致
        courseDescription.setId(eduCourse.getId());
        eduCourseDescriptionService.save(courseDescription);
        return courseId;
    }

    @Override
    public CourseInfoVo getCourseInfo(String courseId) {
        CourseInfoVo courseInfoVo=new CourseInfoVo();
        //封装课程表的信息
        EduCourse eduCourse = baseMapper.selectById(courseId);
        BeanUtils.copyProperties(eduCourse,courseInfoVo);
        //封装课程描述表的信息
        EduCourseDescription description = eduCourseDescriptionService.getById(courseId);
        courseInfoVo.setDescription(description.getDescription());
        return courseInfoVo;
    }

    @Override
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {
        //更新课程基本信息
        EduCourse eduCourse=new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int update = baseMapper.updateById(eduCourse);
        if(update == 0){
            throw new GuliException(20001,"课程信息更新失败");
        }
        //更新描述信息
        EduCourseDescription description=new EduCourseDescription();
        description.setId(courseInfoVo.getId());
        description.setDescription(courseInfoVo.getDescription());
        eduCourseDescriptionService.updateById(description);
    }

    @Override
    public CoursePublishVo getPublishCourseInfo(String courseId) {
        CoursePublishVo publishCourseInfo = baseMapper.getPublishCourseInfo(courseId);
        return publishCourseInfo;
    }

    /**
     * 对于多表的删除，建议按顺序来删除
     * 对于一对多关系，不建议用外键进行维护，外键维护的话删除数据可能会失败
     * @param courseId
     */
    @Override
    public void deleteCourseById(String courseId) {
        //1、根据课程id删除小节
        eduVideoService.deleteByCourseId(courseId);
        //2、根据课程id删除章节
        eduChapterService.deleteByCourseId(courseId);
        //3、根据课程id删除描述
        eduCourseDescriptionService.removeById(courseId);
        //4、删除课程
        int delete = baseMapper.deleteById(courseId);
        if(delete <=0){
            throw new GuliException(20001,"课程删除失败");
        }
    }

    /**
     * 课程列表页面根据多条件查询
     * @param coursePage
     * @param vo
     * @return
     */
    @Override
    public Map<String, Object> getCourseFrontList(Page<EduCourse> coursePage, CourseFrontVo vo) {
        QueryWrapper<EduCourse> wrapper=new QueryWrapper<>();
        //一级分类
        if(!StringUtils.isEmpty(vo.getSubjectParentId())){
            wrapper.eq("subject_parent_id",vo.getSubjectParentId());
        }
        //二级分类
        if(!StringUtils.isEmpty(vo.getSubjectId())){
            wrapper.eq("subject_id",vo.getSubjectId());
        }
        //关注度排序
        if(!StringUtils.isEmpty(vo.getBuyCountSort())){
            wrapper.orderByDesc("buy_count");
        }
        //最新排序
        if(!StringUtils.isEmpty(vo.getGmtCreateSort())){
            wrapper.orderByDesc("gmt_create");
        }
        //价格排序
        if(!StringUtils.isEmpty(vo.getPriceSort())){
            wrapper.orderByDesc("price");
        }
        baseMapper.selectPage(coursePage, wrapper);
        List<EduCourse> records = coursePage.getRecords();
        long current = coursePage.getCurrent();
        long pages = coursePage.getPages();
        long size = coursePage.getSize();
        long total = coursePage.getTotal();
        boolean hasNext = coursePage.hasNext();
        boolean hasPrevious = coursePage.hasPrevious();

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);
        return map;
    }

    @Override
    public CourseWebVo getBaseCourseInfo(String courseId) {
        CourseWebVo courseWebVo=baseMapper.getBaseCourseInfo(courseId);
        return courseWebVo;
    }
}
